package com.ruoyi.binance.task;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binance.api.KlinesApi;
import com.ruoyi.binance.domain.*;
import com.ruoyi.binance.mapper.*;
import com.ruoyi.binance.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Component
public class DayMarketTask {

    private static final Logger log = LoggerFactory.getLogger(DayMarketTask.class);
    @Autowired
    private IBinanceDailyMarketService binanceDailyMarketService;
    @Autowired
    private IBinanceMinKConfService binanceMinKConfService;
    @Autowired
    private IBinanceAverageBuySellService binanceAverageBuySellService;
    @Autowired
    private IBinanceAutoSingleCellService binanceAutoSingleCellService;

    @Scheduled(cron = "0/20 59 07 * * ?")
    public void tradingDay() {
        try {
            log.info("日交易行情定时任务----------------------开始");
            List<BinanceMinKConf> priceMonitorings = binanceMinKConfService.selectBinanceMinKConfList(new BinanceMinKConf());
            for (BinanceMinKConf bean : priceMonitorings) {
                JSONObject jsonObject = KlinesApi.tradingDay(bean.getCode());
                if (jsonObject == null) {
                    continue;
                }
                binanceDailyMarketService.saveOrUpdateTradingDay(bean.getCode(), jsonObject);
            }
            log.info("日交易行情定时任务----------------------结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Scheduled(cron = "0 01 08 * * ?")
    public void highAndLowRange() {
        try {
            log.info("自动计算均价涨跌幅----------------------开始");
            BinanceAverageBuySell averageBuySell = new BinanceAverageBuySell();
            averageBuySell.setAuto("0");
            List<BinanceAverageBuySell> binanceAverageBuySells = binanceAverageBuySellService.selectBinanceAverageBuySellList(averageBuySell);
            for (BinanceAverageBuySell bean : binanceAverageBuySells) {
                binanceDailyMarketService.autoHighAndLowRange(bean);
            }
            List<BinanceAutoSingleCell> binanceAutoSingleCells = binanceAutoSingleCellService.selectBinanceAutoSingleCellList(new BinanceAutoSingleCell());
            for (BinanceAutoSingleCell bean : binanceAutoSingleCells) {
                binanceDailyMarketService.autoHighAndLowRange(bean);
            }
            log.info("自动计算均价涨跌幅----------------------结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
